package com.huiquan.analysis.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huiquan.analysis.constants.AnalysisDictionaryConstant;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.analysis.service.AnalysisDictionaryService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;

/**
 * 词典 controller
 *
 * @author leo
 * @time 2017年4月12日 上午11:52:40
 */
@RequestMapping("/analysis/dictionary")
@Controller
public class AnalysisDictionaryController {
	@Autowired
	private AnalysisDictionaryService service;

	/**
	 * 初始化数据
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("init_data.do")
	@Deprecated
	public ReturnData initData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		return service.initData(user);
	}

    /**
     * 同步词典数据
     *
     * @throws IOException
     */
    @RequestMapping("synchronization_data.do")
    public void synchronizationData() throws IOException {
        // 同步字典到181
        service.synchronizationDictionary();
    }

    /**
     * 查看词典列表
     *
     * @param req
     * @param resp
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/view_dictionary_list.htm")
    public ModelAndView viewDictionaryList(HttpServletRequest req, HttpServletResponse resp) {
        String tabid = RequestParamUtil.getTrimString(req, "tabid");
        String secondType = req.getParameter("secondType");
        if (StringUtils.isEmpty(secondType)) {
            secondType = "zs";
        }
        Map<String, Object> pageParam = new HashMap<>();
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            pageParam.put("secondType", secondType);
            pageParam.put("exactSearchFlag", "0");

            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
            tabid = String.valueOf(pageParam.get("tabid"));
            if (tabid.equals("33_1") || tabid.equals("33_2")) {
                secondType = pageParam.get("secondType").toString();
            }
        }

        int start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
        String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
        String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
        Map<String, Object> map = service.viewDictionaryList(tabid, start, keyword, secondType,exactSearchFlag);
        return new ModelAndView("dictionary/index", map);
    }
    
    /**
     * 查看歧义词典列表
     *
     * @param req
     * @param resp
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping("/view_element_split_ambiguity_list.htm")
    public ModelAndView viewElementSplitAmbiguityList(HttpServletRequest req, HttpServletResponse resp) {
        String tabid = RequestParamUtil.getTrimString(req, "tabid");
        String secondType = req.getParameter("secondType");
        String elementSplitAmbiguity = "1";
        if (StringUtils.isEmpty(secondType)||secondType==null) {
            secondType = "zs";
        }
        Map<String, Object> pageParam = new HashMap<>();
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            pageParam.put("secondType", secondType);
            pageParam.put("elementSplitAmbiguity", elementSplitAmbiguity);
            pageParam.put("exactSearchFlag", "0");

            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
            tabid = String.valueOf(pageParam.get("tabid"));
            secondType = pageParam.get("secondType").toString();           
        }

        int start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
        String keyword = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "keyword");
        pageParam.put("keyword", keyword);
        String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
        Map<String, Object> map = service.viewElementSplitAmbiguityList(tabid, start, keyword, secondType,exactSearchFlag);
        return new ModelAndView("dictionary/index", map);
    }

    /**
     * 更新一条词典
     *
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody
    @RequestMapping("/update_dictionary.do")
    public ReturnData updateDictionary(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        Map<String, String> param = new HashMap<>();
        param.put("id", RequestParamUtil.getTrimString(req, "id"));
        param.put("vocabulary", RequestParamUtil.getTrimString(req, "vocabulary"));
        param.put("dictionary", RequestParamUtil.getTrimString(req, "dictionary"));
        return service.updateDictionary(param, user);
    }

    /**
     * 删除一条词典
     *
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete_dictionary.do")
    public ReturnData deleteDictionary(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        int id = RequestParamUtil.getIntegerDefaultDi(req, "id", 0);
        return service.deleteDictionaryById(id, user);
    }

    /**
     * 添加一条词典
     *
     * @param req
     * @param resp
     * @return
     */
    @ResponseBody
    @RequestMapping("/add_dictionary.do")
    public ReturnData addDictionary(HttpServletRequest req, HttpServletResponse resp) {
        User user = (User) req.getSession().getAttribute("user");
        @SuppressWarnings("unchecked")
        Map<String, Object> pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        String tabid = String.valueOf(pageParam.get("tabid"));
        String secondType = String.valueOf(pageParam.get("secondType"));
        @SuppressWarnings("unchecked")
        Map<String, Object> map = req.getParameterMap();
        String[] dictionary = (String[]) map.get("dictionary");
        String[] characterStrs = (String[]) map.get("character");
        Map<String, String> param = new HashMap<>();
        param.put("tabid", tabid);
        param.put("secondType", secondType);
        param.put("vocabulary", RequestParamUtil.getTrimString(req, "vocabulary"));

        return service.addDictionary(param, user, dictionary, characterStrs);
    }
}
